<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\LotteryResult;
use App\Services\RedisService;

use Log,Session;

class LotteryResultController extends Controller
{
    public function index(Request $request)
    {  
        $param = [];
        if($request->openid) {
            $param['openid'] = $request->openid;
        }

        if($request->gift_type) {
            $param['gift_type'] = (int)$request->gift_type;
        }

        if($request->gift_id) {
            $param['gift_id'] = (int)$request->gift_id;
        }

        if($request->mobile) {
            $param['mobile'] = trim($request->mobile);
        }
        
        $list = LotteryResult::where($param)->orderBy('created_at','desc')->paginate();
    	return view('admin.result.index',compact('list','param'));
    }

    public function lotteryInfo($id)
    {
        $data = LotteryResult::find($id);
        return view('admin.converte',compact('data'));
    }

    /**
     * 兑奖
     */
    public function lotteryConverte(Request $request)
    {
        $rs['status'] = false;
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token']);
        $data['is_converted'] = 1;
        $data['converted_at'] = date('Y-m-d H:i:s');
        $flag = LotteryResult::where('id',$data['id'])->update($data);
        if($flag) {
            $rs['status'] = true;
            $rs['msg']    = '兑奖成功';
        }
        return redirect('zadmin/')->with('rs',$rs);
    }

    private function getResultByType($type)
    {
    	$where = [];
        //$where['gift_type'] = $type;
    	return LotteryResult::where($where)
        					->count();
    }
}
